package com.leizi.igym.utils;

import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-02  18:11
 * @Description:
 */

public class FileUtils {
    public static Boolean fileValid(MultipartFile file) throws BusinessException {
        // 文件大小不超过2MB
        if (file.getSize() > 1024 * 2) {
            throw new BusinessException(ReturnCodeEnum.FILE_OVER_SIZE);
        }
        if (!Objects.requireNonNull(file.getContentType()).startsWith("image/")) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR.getCode(), "文件类型不正确");
        }
        return true;
    }
}
